package com.pb.infra.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.auth.entity.pbsys.PriviPosition;
import com.pb.infra.auth.entity.pbsys.PriviPositionExample;
import com.pb.infra.auth.mapper.pbsys.PriviPositionMapper;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.entity.pbsys.PriviPost;
import com.pb.infra.auth.mapper.custom.PriviPostCustomMapper;
import com.pb.infra.auth.mapper.pbsys.PriviPostMapper;
import com.pb.infra.oth.service.SequenceGeneratorService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.DateTimeUtil;
import com.pb.infra.oth.util.StringUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
 * 岗位管理
 *
 * @author liudebao
 * @date 2020/12/25
 */
@Service
public class PriviPostService {

    /**
     * 岗位单表DAO映射
     */
    @Resource
    private PriviPostMapper priviPostMapper;

    /**
     * 岗位多表DAO映射
     */
    @Resource
    private PriviPostCustomMapper priviPostCustomMapper;

    /**
     * 序号自动生成逻辑层
     */
    @Resource
    private SequenceGeneratorService sequenceGeneratorService;

    /**
     * 职位表DAO映射
     */
    @Resource
    private PriviPositionMapper priviPositionMapper;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询岗位信息
     *
     * @param map
     * @return
     */
    public Result select(Map<String, Object> map, Page page, HttpServletRequest request) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        if (!StringUtil.isEmpty(map.get("loginUser")) && Constant.OK.equals(map.get("loginUser").toString())) {
            String token = request.getHeader(jwtTokenConfig.getHeader());
            Claims claims = jwtTokenConfig.getClaimByToken(token);
            String userId = (String)claims.get(jwtTokenConfig.getSub());
            map.put("loginUser", userId);
        }
        List<Map<String, Object>> list = priviPostCustomMapper.getPost(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }


    /**
     * 保存岗位信息
     *
     * @param priviPost 请求参数
     */
    @Transactional
    public void insert(PriviPost priviPost, HttpServletRequest request) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        String seqId = sequenceGeneratorService.getNextIdStr("POST", "POST");
        priviPost.setPostId(seqId);
        priviPost.setCreateUser(userId);
        priviPost.setCreateTime(DateTimeUtil.getStrFromNewDate());
        priviPost.setUpdateUser(userId);
        priviPost.setUpdateTime(DateTimeUtil.getStrFromNewDate());
        priviPostMapper.insertSelective(priviPost);
    }

    /**
     * 修改岗位信息
     *
     * @param priviPost 请求参数
     */
    @Transactional
    public void update(PriviPost priviPost, HttpServletRequest request) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        priviPost.setUpdateUser(userId);
        priviPost.setUpdateTime(DateTimeUtil.getStrFromNewDate());
        priviPostMapper.updateByPrimaryKeySelective(priviPost);
    }

    /**
     * 删除岗位信息
     *
     * @param map 请求参数
     */
    @Transactional
    public boolean delete(List<String> map) {
        // 删除参数
        boolean flag = false;
        for (String appId : map) {
            PriviPositionExample priviPositionExample = new PriviPositionExample();
            PriviPositionExample.Criteria criteria = priviPositionExample.createCriteria();
            criteria.andPostIdEqualTo(appId);
            criteria.andDeleteFlagEqualTo("0");
            List<PriviPosition> list = priviPositionMapper.selectByExample(priviPositionExample);
            if (list != null && list.size() > 0) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            for (String appId : map) {
                PriviPost priviPost = new PriviPost();
                priviPost.setPostId(appId);
                priviPost.setDeleteFlag(Constant.EXIST);
                priviPostMapper.updateByPrimaryKeySelective(priviPost);
                //priviPostMapper.deleteByPrimaryKey(appId);
                //priviPostCustomMapper.updatePost(appId);
            }
        }
        return flag;
    }

    /**
     * 恢复岗位
     * @param map
     */
    @Transactional
    public void updateFlag(List<String> map) {
        for (String appId : map) {
            PriviPost priviPost = new PriviPost();
            priviPost.setPostId(appId);
            priviPost.setDeleteFlag(Constant.OK);
            priviPostMapper.updateByPrimaryKeySelective(priviPost);
            //priviOrgMapper.deleteByPrimaryKey(appId);
        }
    }

}
